//
//  LBCellBuilder.m
//  jslj
//
//  Created by LINAICAI on 2017/9/29.
//  Copyright © 2017年 广东联结电子商务有限公司. All rights reserved.
//

#import "LBCellBuilder.h"
#import "LBCellItemBuilder.h"
#import <LBCoreKit/LBCoreKit.h>
#import <LBCoreKit/LBPrefix.h>
#import <Masonry/Masonry.h>
#import <LBCategory/LBCategory.h>

 NSUInteger const kLeftMargin = 60;    //默认的左边距
 NSUInteger const kRightMargin = 20;    //默认的右边距

 NSUInteger const kLeftMarginNoImage = 20;    //默认的左边距(没有图片时)
 NSUInteger const kRightMarginNoImage = 20;    //默认的右边距(没有图片时)
@interface LBCellBuilder()

@end

@implementation LBCellBuilder
- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.font = LBFONT(16);
        _titleLabel.textColor = LBCOLOR(68, 68, 68);
    }
    return _titleLabel;
}
- (UIImageView *)iconImageView{
    if (!_iconImageView) {
        _iconImageView = [[UIImageView alloc]init];
        _iconImageView.contentMode = UIViewContentModeCenter;
    }
    return _iconImageView;
}
- (UIView *)rightView{
    if (!_rightView) {
        _rightView = [[UIView alloc]init];
    }
    return _rightView;
}
- (UIImageView *)arrowImageView{
    if (!_arrowImageView) {
        
        _arrowImageView = [[UIImageView alloc]initWithImage:[[UIImage imageNamed:@"LBCellBuilder.bundle/lb_user_me_arrow"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]];
        _arrowImageView.contentMode = UIViewContentModeScaleAspectFill;
        _arrowImageView.tintColor = LBCOLOR(179, 179, 179);
        
    }
    return _arrowImageView;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(nullable NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.iconImageView];
        
        [self.contentView addSubview:self.arrowImageView];
        
        [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(30, 30));
            make.left.mas_offset(kLeftMarginNoImage);
            make.centerY.equalTo(self.contentView);
        }];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.contentView);
            make.left.mas_equalTo(self.iconImageView.mas_right).mas_offset(20);
            make.right.lessThanOrEqualTo(self.contentView);
        }];
        [self.arrowImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(15, 20));
            make.right.mas_equalTo(self.contentView.mas_right).mas_offset(-18.5);
            make.centerY.equalTo(self.contentView);
        }];

    }
    return self;
}
- (void)setItem:(LBCellItemBuilder *)item{
    self.accessoryType = UITableViewCellAccessoryNone;
    self.titleLabel.text = item.title;
    self.iconImageView.image = item.image;
    if (!item.image) {
        [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.contentView);
            make.left.mas_offset(kLeftMarginNoImage);
            make.right.lessThanOrEqualTo(self.contentView);
        }];
    }
    if (item.style & LBCellItemBuilderStyleNone) {
        self.arrowImageView.alpha = 0.0;
        [self.contentView addSubview:self.rightView];
        [self.rightView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeZero);
            make.centerY.equalTo(self.contentView);
            make.right.mas_equalTo(self.contentView.mas_right).mas_offset(-10);
        }];
        [self.arrowImageView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(0);
        }];
    }
    if (item.style & LBCellItemBuilderStyleArrow) {
        self.arrowImageView.alpha = 1.0;
        [self.contentView addSubview:self.rightView];
        [self.rightView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeZero);
            make.centerY.equalTo(self.contentView);
            make.right.mas_equalTo(self.arrowImageView.mas_left).mas_offset(-10);
        }];
        [self.arrowImageView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(15);
        }];
    }
    if (item.style & LBCellItemBuilderStyleBadge) {
        self.badgeView.alpha = 1.0;
        
        UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"LBCellBuilder.bundle/lb_user_me_badge"]];
        imageView.frame = CGRectMake(0, 0, 10, 10);
        
        self.rightView = imageView;
        [self.contentView addSubview:self.rightView];
        
        [self.arrowImageView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(15);
        }];
        [self.rightView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(imageView.bounds.size);
            make.centerY.equalTo(self.contentView);
            make.right.mas_equalTo(self.arrowImageView.mas_left).mas_offset(0);
        }];
        
    }
    if (item.style & LBCellItemBuilderStyleString) {
        self.badgeView.alpha = 0.0;
        
        UILabel *label = [[UILabel alloc]init];
        label.text = item.attachedField;
        label.font = [UIFont systemFontOfSize:15];
        label.textColor = [UIColor black];
        [label sizeToFit];
        
        
        self.rightView = label;
        [self.contentView addSubview:self.rightView];
        [self.arrowImageView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(15);
        }];
        [self.rightView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(label.bounds.size);
            make.centerY.equalTo(self.contentView);
            make.right.mas_equalTo(self.arrowImageView.mas_left).mas_offset(0);
        }];
    }
    if (item.style & LBCellItemBuilderStyleCustomView) {
        self.badgeView.alpha = 0.0;
        
        UIView *custom = (UIView*)item.attachedField;
        
        self.rightView = item.attachedField;
        [self.contentView addSubview:self.rightView];
        [self.arrowImageView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(15);
        }];
        [self.rightView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(custom.bounds.size);
            make.centerY.equalTo(self.contentView);
            make.right.mas_equalTo(self.arrowImageView.mas_left).mas_offset(0);
        }];
    }
    
}
- (void)awakeFromNib {
    [super awakeFromNib];
    
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
}

@end
